// npm install -g crypto-js
const CryptoJS = require('crypto-js')
// npm install moment
const moment = require('moment');
window = global

function get_fp_sign() {
    let r = {
        "sua": "Windows NT 10.0; Win64; x64",
        "pp": {},
        "fp": "2056554713322828"
    }
    let i = JSON.stringify(r, null, 2),
        //this[n(0, 987, 718)]("__collect envCollect=".concat(i)),
        o = CryptoJS["AES"]["encrypt"](i, CryptoJS["enc"]["Utf8"]["parse"]("wm0!@w_s#ll1flo("), {
            iv: CryptoJS["enc"]["Utf8"]['parse']('0102030405060708'),
            mode: CryptoJS["mode"].CBC,
            padding: CryptoJS["pad"]["Pkcs7"]
        }),
        fp_sign = o["ciphertext"]["toString"]();

    return fp_sign
}

function get_h5st(n, fp_sign, body_str) {
    let u = ""
        , t = Date['now']()
        , ts = moment().format('yyyyMMDDHHmmssSSS')
        , tk = "tk03wb4aa1bdd18nw41dprbJ8MgdByCbxiKHO9GcPGUneem6zwzjlohi64W4g47ENYBVCFzHri01NfTFJ0U3vtU82Mce"
        , fp = "2056554713322828"
        , appid = "586ae";

    // let rd = '0Khys6jE6FcG';
    let rd = "jo1DNcD3XHU6"
    var str = "".concat(tk).concat(fp).concat(ts).concat(appid).concat(rd);
    // u = CryptoJS.SHA512(str).toString();
    u = CryptoJS.HmacSHA512(str, tk).toString();
    // u = CryptoJS.HmacMD5(str, tk).toString();
    console.log(u);
    let cc = "appid:unionpc&body:" + body_str + "&functionId:unionSearch"
    let sign = CryptoJS.HmacSHA256(cc, u).toString((CryptoJS.enc["Hex"]));
    console.log(sign)
    let version = '3.1'
    // 日期 + 环境fp值 + appid + token + 参数签名 + 版本号 + 时间戳 + 环境参数加密字符串
    let h5st = [""['concat'](ts), ""['concat'](fp), ""['concat'](appid), ""['concat'](tk), ""['concat'](sign), ""['concat'](version), ""['concat'](t), "".concat(fp_sign)].join(";")

    let m = {
        key: fp_sign,
        signStr: sign,
        _stk: "appid,body,functionId",
        _ste: 1,
        h5st: h5st
    }
    return m;

}

function get_data(param) {
    let fp_sign = get_fp_sign()
    let str = JSON.stringify(param)
    let body_str = CryptoJS.SHA256(str).toString()
    let p = [
        {
            "key": "appid",
            "value": "unionpc"
        },
        {
            "key": "body",
            "value": body_str
        },
        {
            "key": "functionId",
            "value": "unionSearch"
        }
    ]
    return get_h5st(p, fp_sign, body_str)
}

// console.log(get_data({
//     "funName": "querySearchInfoByEliteId",
//     "param": {
//         "pageNo": 1,
//         "pageSize": 60,
//         "eliteId": 22
//     },
//     "clientPageId": "jingfen_pc"
// }))


